$(document).ready(function() {
	// $("form").submit(function() {
	// 	return false;
	// });
	var VAL = {
		NORMAL : "0000000",
		EVT_USER : $("#userId").text(),
		T_FSPFUTACTION : "FSPFUTACTION",
		T_FBPMDLDEF	: "FBPMDLDEF",
		T_FBPBISDAT : "FBPBISDAT",
		T_FDPAPLYWO : "FDPAPLYWO",
		T_FBPBISPTH : "FBPBISPTH",
		T_FIPINQSHT : "FIPINQSHT",
        T_FBPBISBOM : "FBPBISBOM",
        T_FBPBISTOL : "FBPBISTOL",
        T_FBPBISOPE : "FBPBISOPE"

	};
	var domObj = {
		$window : $(window),
        $reservation_type : $("#reservation_type"), //预约类型
		$product_code	  : $("#product_code"),		//产品代码
        $route_code		  : $("#route_code"),		//工艺代码
		$route_ver		  : $("#route_ver"),		//工艺版本
		$ope_id			  : $("#ope_id"),			//制程代码
		$bad_id		  	  : $("#bad_id"),			//不良
		$wo_id			  : $("#wo_id"),			//工单号
        $tool_id          : $("#tool_id"),          //设备
        $grade            : $("#grade"),            //等级
		$lot_id			  : $("#lot_id"),			//批次号
		$reason_id		  : $("#reason_id"),		//原因代码
		$ope_no			  : $("#ope_no"),			//操作代码
        $ope_note         : $("#ope_note"),         //备注
 		$prd_seq_id		  : $("#prd_seq_id"),		//产品唯一码
		$radioGroup		  : $("#radioGroup"),		//radio组
        $checkList        : $("#checkList"),        //预约子格位
		$rightDiv		  : $("#rightDiv"),			//预约方案整体
		$res_product_code : $("#res_product_code"), //预约产品代码
		$reserWorkId	  : $("#reserWorkId")  ,    //预约工单号
        $res_path_id      : $("#res_path_id"),      //预约工艺路线
        $res_path_ver     : $("#res_path_ver"),     //预约工艺版本
        $res_ope_no       : $("#res_ope_no"),       //预约操作代码
        $res_ope_id       : $("#res_ope_id"),       //预约制程代码
        $res_tool         : $("#res_tool"),         //预约设备代码


		grids : {
			$prdInfoGrd : $("#prdInfoGrd"),
		},

		buttons : {
			$reser_list : $("#reser_list"),          //获取预约列表
            $login_btn  : $("#login_btn"),           //登记
            $active_btn : $("#active_btn"),          //激活
            $remove_btn : $("#remove_btn")           //删除

		},

	};
	function initPrdInfoGrd() {
		domObj.grids.$prdInfoGrd.jqGrid({
			url : "",
			datatype : "local",
			mtype : "POST",
			height : 200,
			width : $("#prdInfoGrdDiv").width ,
			autowidth : false,
			shrinkToFit : true,
			resizable : true,
			loadonce : true,
			viewrecords : true,
			rownumbers : true,
			rownumWidth : 30,
			rowNum : 20,
			multiselect: true,
			emptyrecords : true,
			//multiselect : true,
			colModel : [ {
				name : 'valid_flg',
				index : 'valid_flg',
				label : '激活标识',
				width : 60
			}, {
				name : 'rul_act_code',
				index : 'rul_act_code',
				label : '预约类型',
				width : 70
			}, {
				name : 'mdl_id_fk',
				index : 'mdl_id_fk',
				label : '产品代码',
				width : 70
			}, {
				name : 'path_id_fk',
				index : 'path_id_fk',
				label : '工艺路线',
				width : 70
			}, {
				name : 'path_ver_fk',
				index : 'path_ver_fk',
				label : '工艺版本',
				width : 70
			}, {
				name : 'ope_no_fk',
				index : 'ope_no_fk',
				label : '操作代码',
				width : 70
			}, {
				name : 'ope_id_fk',
				index : 'ope_id_fk',
				label : '制程代码',
				width : 70
			}, {
				name : 'tool_id_fk',
				index : 'tool_id_fk',
				label : '设备代码',
				width : 70
			},
			{
				name : 'wo_id_fk',
				index : 'wo_id_fk',
				label : '工单号',
				width : 70
			},
			{
				name : 'rsn_code',
				index : 'rsn_code',
				label : '原因代码',
				width : 70
			},
			{
				name : 'to_mdl_id_fk',
				index : 'to_mdl_id_fk',
				label : '预约产品代码',
				width : 90
			},
			{
				name : 'to_path_id_fk',
				index : 'to_path_id_fk',
				label : '预约工艺路线',
				width : 90
			},
			{
				name : 'to_path_ver_fk',
				index : 'to_path_ver_fk',
				label : '预约工艺版本',
				width : 90
			},
			{
				name : 'to_ope_no_fk',
				index : 'to_ope_no_fk',
				label : '预约操作代码',
				width : 90
			},
			{
				name : 'to_ope_id_fk',
				index : 'to_ope_id_fk',
				label : '预约制程代码',
				width : 90
			},
			{
				name : 'to_tool_id_fk',
				index : 'to_tool_id_fk',
				label : '预约设备代码',
				width : 90
			},{
				name : 'to_wo_id_fk',
				index : 'to_wo_id_fk',
				label : '预约工单号',
				width : 80
			},{
				name : 'ope_note',
				index : 'ope_note',
				label : '备注',
				width : 60
			},
            {
                name : 'rul_seq_id',
                index : 'rul_seq_id',
                label : '流水号',
                width : 40
            }
			],
			pager : "prdInfoPg",

		});
	};

	var toolFun = {
	    clearFun : function (val_1,val_2,val_3,val_4,val_5,val_6) {
            if (val_1 != null) {
                val_1.empty();
                SelectDom.addSelect(val_1," "," ");
                val_1.select2({
                    theme : "bootstrap"
                });
            }
            if (val_2 != null) {
                val_2.empty();
                SelectDom.addSelect(val_2," "," ");
                val_2.select2({
                    theme : "bootstrap"
                });
            }
            if (val_3 != null) {
                val_3.empty();
                SelectDom.addSelect(val_3," "," ");
                val_3.select2({
                    theme : "bootstrap"
                });
            }
            if (val_4 != null) {
                val_4.empty();
                SelectDom.addSelect(val_4," "," ");
                val_4.select2({
                    theme : "bootstrap"
                });
            }
            if (val_5 != null) {
                val_5.empty();
                SelectDom.addSelect(val_5," "," ");
                val_5.select2({
                    theme : "bootstrap"
                });
            }
            if (val_6 != null) {
                val_6.empty();
                SelectDom.addSelect(val_6," "," ");
                val_6.select2({
                    theme : "bootstrap"
                });
            }

        },
        clearFunNoSpace : function (val_1,val_2,val_3,val_4,val_5,val_6) {
            if (val_1 != null) {
                val_1.empty();
                val_1.select2({
                    theme : "bootstrap"
                });

            }
            if (val_2 != null) {
                val_2.empty();
                val_2.select2({
                    theme : "bootstrap"
                });
            }
            if (val_3 != null) {
                val_3.empty();
                val_3.select2({
                    theme : "bootstrap"
                });
            }
            if (val_4 != null) {
                val_4.empty();
                val_4.select2({
                    theme : "bootstrap"
                });
            }
            if (val_5 != null) {
                val_5.empty();
                val_5.select2({
                    theme : "bootstrap"
                });
            }
            if (val_6 != null) {
                val_6.empty();
                val_6.select2({
                    theme : "bootstrap"
                });
            }

        },
	    initReserByCon : function () {
            if (domObj.$product_code.val() != ""){
                var mdl_id = domObj.$product_code.val();
                toolFun.initResProdut(mdl_id);
            } else if (domObj.$product_code.val() == "" && domObj.$wo_id.val() != ""){
                var mdl_id = toolFun.findProctByWo();
                toolFun.initResProdut(mdl_id);
            } else {
                domObj.$res_product_code.empty();
                SelectDom.addSelect(domObj.$res_product_code,"","");
            }
        },
		initProgram : function () {
            if ("FTHL" == domObj.$reservation_type.val()){
                domObj.$rightDiv.hide();
            } else {
                domObj.$rightDiv.show();
			}
        },
		initReserTyp : function () {
            if ("FTHL" != domObj.$reservation_type.val()){
                domObj.$radioGroup.hide();
            } else {
                domObj.$radioGroup.show();
            }
        },
        initReser : function () {
            var inObj,
                outObj,
                iary,
                iary = {};
            inObj = {
                trx_id: VAL.T_FBPMDLDEF,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj != null) {
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                    for (var i = 0; i < outObj.oary.length; i++) {
                        SelectDom.addSelect(domObj.$product_code, outObj.oary[i].mdl_id, outObj.oary[i].mdl_id);
                    }
                }
            }
            }
        },
		initReason : function () {
            var inObj,
                outObj,
                iary;
            iary = {
            	data_cate : "FTRS",
				data_id : domObj.$reservation_type.val()
            };
            inObj = {
                trx_id: VAL.T_FBPBISDAT,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj != null) {
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                	for (var i = 0;i < outObj.oary.length;i++) {
                        SelectDom.addSelect(domObj.$reason_id,outObj.oary[i].data_ext,outObj.oary[i].data_desc);
					}

                }
            }
            }
        },
        initOpe_no : function () {
            var inObj,
                outObj,
                iary;
            var iaryA = {};
            var iaryB = {};
            if (domObj.$route_code.val() != ""  && domObj.$route_ver.val() != "") {
                iaryA.path_id    = domObj.$route_code.val();
                iaryB.path_id_fk = domObj.$route_code.val();
                iaryA.path_ver    = domObj.$route_ver.val();
                iaryB.path_ver_fk = domObj.$route_ver.val();
			} else {
            	return false;
			}
            inObj = {
                trx_id: VAL.T_FBPBISPTH,
                action_flg: 'Q',
                iaryA      : [iaryA]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt_b > 0) {
					for (var i = 0;i < outObj.oaryB.length;i++) {
						SelectDom.addSelect(domObj.$ope_no,outObj.oaryB[i].cr_ope_no,outObj.oaryB[i].cr_ope_no);
					}
                }
            }
        },
        initOpe_id : function () {
            var inObj,
                outObj,
                iary;
            var iaryB = {};
            if (domObj.$route_code.val() != ""  && domObj.$route_ver.val() != "" && domObj.$ope_no.val() != "") {
                iaryB.path_id_fk = domObj.$route_code.val();
                iaryB.path_ver_ver = domObj.$route_ver.val();
                iaryB.cr_ope_no = domObj.$ope_no.val();
            } else if(domObj.$route_code.val() == ""  && domObj.$route_ver.val() == "" && domObj.$ope_no.val() == "") {
                iaryB.path_id = "";
                iaryB.path_ver = "";
                iaryB.cr_ope_no = "";
            } else {
            	return false;
			}
            inObj = {
                trx_id: VAL.T_FBPBISPTH,
                action_flg: 'I',
                iaryB      : [iaryB]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt_b > 0) {
                    for (var i = 0;i < outObj.oaryB.length;i++) {
                        SelectDom.addSelect(domObj.$ope_id,outObj.oaryB[i].cr_ope_id_fk,outObj.oaryB[i].cr_ope_id_fk);
                    }
                }
            }
        },
        initResProdut : function (mdl_id) {
            var inObj,
                outObj,
                iary,
                iary = {
                    mtrl_cate : "BARE",
                    mdl_id_fk : mdl_id
                };
            inObj = {
                trx_id: VAL.T_FBPBISBOM,
                action_flg: 'I',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                    for (var i = 0; i < outObj.oary.length; i++) {
                        SelectDom.addSelect(domObj.$res_product_code,outObj.oary[i].mdl_id_fk,outObj.oary[i].mdl_id_fk);
                    }
                }
            }
        },
        initResPathId : function () {
            var inObj,
                outObj,
                iary,
                iary = {
                    mdl_id: domObj.$product_code.val()
                }
            inObj = {
                trx_id: VAL.T_FBPMDLDEF,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                    var pathIdFk = outObj.oary[0].path_id_fk;
                        toolFun.checkPathId(pathIdFk,null);
                }
            }
        },
        initResPathVer : function () {
            var inObj,
                outObj,
                iaryA,
                iaryA = {
                    path_id: domObj.$res_path_id.val(),
                }
            inObj = {
                trx_id: VAL.T_FBPBISPTH,
                action_flg: 'Q',
                iaryA: [iaryA]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt_a > 0) {
                    for (var i = 0; i < outObj.oaryA.length; i++){
                        SelectDom.addSelect(domObj.$res_path_ver,outObj.oaryA[i].path_ver,outObj.oaryA[i].path_ver);
                    }

                }
            }
        },
        initResOpeNo : function () {
            var inObj,
                outObj,
                iary;
            var iaryA = {};
            var iaryB = {};
            if (domObj.$res_path_id.val() != ""  && domObj.$res_path_ver.val() != "") {
                iaryA.path_id    = domObj.$res_path_id.val();
                iaryB.path_id_fk = domObj.$res_path_id.val();
                iaryA.path_ver    = domObj.$res_path_ver.val();
                iaryB.path_ver_fk = domObj.$res_path_ver.val();
            } else {
                return false;
            }
            inObj = {
                trx_id: VAL.T_FBPBISPTH,
                action_flg: 'Q',
                iaryA      : [iaryA]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt_b > 0) {
                    for (var i = 0;i < outObj.oaryB.length;i++) {
                        SelectDom.addSelect(domObj.$res_ope_no,outObj.oaryB[i].cr_ope_no,outObj.oaryB[i].cr_ope_no);
                    }
                }
            }
        },
        initResOpe_id : function () {
            var inObj,
                outObj,
                iary;
            var iaryB = {};
            if (domObj.$res_path_id.val() != ""  && domObj.$res_path_ver.val() != "" && domObj.$res_ope_no.val() != "") {
                iaryB.path_id_fk = domObj.$res_path_id.val();
                iaryB.path_ver_ver = domObj.$res_path_ver.val();
                iaryB.cr_ope_no = domObj.$res_ope_no.val();
            } else {
                return false;
            }
            inObj = {
                trx_id: VAL.T_FBPBISPTH,
                action_flg: 'I',
                iaryB      : [iaryB]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt_b > 0) {
                    for (var i = 0;i < outObj.oaryB.length;i++) {
                        SelectDom.addSelect(domObj.$res_ope_id,outObj.oaryB[i].cr_ope_id_fk,outObj.oaryB[i].cr_ope_id_fk);
                    }
                }
            }
        },
        /*initTool : function () {
            var inObj, outObj;
            var item = "tool_id";
            var iary = {
                //tool_run_mode : domObj.$ope_id.val(),
                tool_stat     : 'RUN'
            };
            inObj = {
                trx_id      : VAL.T_FBPBISTOL,
                action_flg  : 'H',
                iary		 : [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var tbl_cnt = comParseInt( outObj.tbl_cnt_B);
                if( tbl_cnt == 1 ){
                    SelectDom.addSelect(domObj.$tool_id,outObj.oaryB[0][item],outObj.oaryB[0][item]);
                }else if(tbl_cnt>1){
                    for(var i=0;i<tbl_cnt;i++){
                        SelectDom.addSelect(domObj.$tool_id,outObj.oaryB[i][item],outObj.oaryB[i][item]);
                    }
                }
            }*/
        initTool : function() {
            var opeId,opeVer,iary,inObj,outObj,toolRunMode,iary2,inObj2,outObj2,tblCnt;
            opeId = domObj.$ope_id.val();
            opeVer;
            iary ={
                ope_id : opeId,
                ope_ver : opeVer
            };

            inObj = {
                trx_id : VAL.T_FBPBISOPE,
                action_flg : "Q",
                iary : [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                if(outObj.oary[0]!=null){
                    toolRunMode=outObj.oary[0].tool_run_mode;
                }else{
                    return;
                }
            }
            //下面是根据toolrunmode查找toolid
            iary2={
                tool_run_mode:toolRunMode
            };

            inObj2 = {
                trx_id : VAL.T_FBPBISTOL,
                action_flg : "H",
                iary : [iary2]
            };
            outObj2 = comTrxSubSendPostJson(inObj2);
            if (outObj2.rtn_code === VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$tool_id, outObj2.oaryB, "tool_id", "tool_id","",true);
            }
        },

        initResTool : function () {
            var inObj, outObj;
            var item = "tool_id";
            var iary = {
                tool_run_mode : domObj.$res_ope_id.val(),
                tool_stat     : 'RUN'
            };
            inObj = {
                trx_id      : VAL.T_FBPBISTOL,
                action_flg  : 'H',
                iary		 : [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var tbl_cnt = comParseInt( outObj.tbl_cnt_B);
                if( tbl_cnt == 1 ){
                    SelectDom.addSelect(domObj.$res_tool,outObj.oaryB[0][item],outObj.oaryB[0][item]);
                }else if(tbl_cnt>1){
                    for(var i=0;i<tbl_cnt;i++){
                        SelectDom.addSelect(domObj.$res_tool,outObj.oaryB[i][item],outObj.oaryB[i][item]);
                    }
                }
            }
        },
        showRoute_code : function () {
            var inObj,
                outObj,
                iary,
                iary = {
                    mdl_id: domObj.$product_code.val()
                }
            inObj = {
                trx_id: VAL.T_FBPMDLDEF,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                    var pathIdFk = outObj.oary[0].path_id_fk;
                    toolFun.checkPathIdNoRes(pathIdFk,"MAN");
                }
            }
        },
        showRoute_ver : function () {
            var inObj,
                outObj,
                iary,
                iary = {
                    path_id_fk: domObj.$route_code.val(),
                    mdl_id: domObj.$product_code.val(),
                    path_cate :"MAN"
                },
                inObj = {
                    trx_id: VAL.T_FBPMDLDEF,
                    action_flg: 'Q',
                    iary: [iary]
                };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                    for (var i = 0; i < outObj.oary.length; i++) {
                        SelectDom.addSelect(domObj.$route_ver, outObj.oary[i].path_ver_fk, outObj.oary[i].path_ver_fk);
                    }
                }
            }
        },
        checkBad : function () {
            var inObj,
                outObj,
                iary
				var bad_arr = [];
				bad_id = domObj.$bad_id.val();
                iary = {
                    data_cate: "DFCT"
                };
                inObj = {
                    trx_id: VAL.T_FBPBISDAT,
                    action_flg: 'Q',
                    iary: [iary]
                };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                    for (var i = 0; i < outObj.oary.length; i++) {
                       if (bad_id == outObj.oary[i].data_ext){
                       		bad_arr.push(outObj.oary[i].data_ext);
					   }
                    }
                    if (!bad_arr.length > 0){
                    	showErrorDialog(bad_id,"不存在");
					}
                }
            }

        },
		checkWoId : function () {
            var inObj,
                outObj,
                iary,
				wo_id = domObj.$wo_id.val(),
				product_id = domObj.$product_code.val(),
            iary = {
                wo_id : wo_id
            };
            inObj = {
                trx_id: VAL.T_FDPAPLYWO,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                	if ("CLOS" == outObj.oary[0].wo_stat){
                        showErrorDialog(outObj.oary[0].wo_stat,"工单号"+wo_id+"状态为CLOS");
                    } else {
                		if (product_id != "" && product_id != outObj.oary[0].mdl_id_fk){
                            showErrorDialog(outObj.oary[0].mdl_id_fk,"产品代码与"+wo_id+"工单中的不一致");
						}
					}
                } else {
                    showErrorDialog(outObj.tbl_cnt,"工单号"+wo_id+"不存在");
				}


             }
        },
		checkPrdId : function () {
            var inObj,
                outObj,
                prd_seq_id = domObj.$prd_seq_id.val();
            inObj = {
                trx_id: VAL.T_FIPINQSHT,
                action_flg: 'G',
                prd_seq_id : prd_seq_id
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var oarys = outObj.oarys;
                    for (var i = 0; i < oarys.length; i++) {
                	if(domObj.$product_code.val() != ""){
                        if (oarys[i].mdl_id_fk != domObj.$product_code.val()){
                            showErrorDialog(oarys[i].mdl_id_fk,"与"+domObj.$product_code.val()+"不一致");
						}
					}
					if (domObj.$wo_id.val() != ""){
                		if (oarys[i].wo_id_fk != domObj.$wo_id.val()){
                            showErrorDialog(oarys[i].wo_id_fk,"与"+domObj.$wo_id.val()+"不一致");
						}
					}

				}
            }

        },
		checkResProId : function () {
			if (domObj.$product_code != domObj.$res_product_code) {
                var inObj,
                    outObj,
					iary;
                iary = {
                    mtrl_cate : "BARE"
				}
                inObj = {
                    trx_id: VAL.T_FIPINQSHT,
                    action_flg: 'Q',
                    iary : [iary]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
					if (outObj.tbl_cnt > 0) {
						for (var i = 0; i < outObj.oary.length; i++) {

						}
					}
                }
			}
        },
		checkResWoId : function () {
            var inObj,
                outObj,
                iary,
                wo_id = domObj.$reserWorkId.val(),
                product_id = domObj.$product_code.val(),
            iary = {
                wo_id : wo_id
            };
            inObj = {
                trx_id: VAL.T_FDPAPLYWO,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                    if ($.trim(wo_id)==""){
                        return false;
                    }
                        if (product_id !=  outObj.oary[0].mdl_id_fk){
                            showErrorDialog( outObj.oary[0].mdl_id_fk,"产品代码与"+wo_id+"工单中的不一致");
                        }
                    } else {
                    showErrorDialog(outObj.tbl_cnt,"工单号"+wo_id+"不存在");
                }


            }
        },
        checkPathId  : function (path_id,path_cate) {
            var inObj,
                outObj,
                iaryA,
                iaryA = {
                    path_id: path_id,
                    path_cate :path_cate
                }
            inObj = {
                trx_id: VAL.T_FBPBISPTH,
                action_flg: 'Q',
                iaryA: [iaryA]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt_a > 0) {
                    var oaryA = outObj.oaryA;
                    for (var i = 0; i < oaryA.length; i++){
                        if ("MAN" == oaryA[i].path_cate) {
                            SelectDom.setSelect(domObj.$res_path_id,oaryA[i].path_id,oaryA[i].path_id);
                        }
                        if ("RWK" == oaryA[i].path_cate) {
                            SelectDom.setSelect(domObj.$res_path_id,oaryA[i].path_id,oaryA[i].path_id);
                        }
                        if ("FTSM" == oaryA[i].path_cate) {
                            SelectDom.setSelect(domObj.$res_path_id,oaryA[i].path_id,oaryA[i].path_id);
                        }
                    }

                }
            }
        },
        checkPathIdNoRes  : function (path_id,path_cate) {
            var inObj,
                outObj,
                iaryA,
                iaryA = {
                    path_id: path_id,
                    path_cate :path_cate
                }
            inObj = {
                trx_id: VAL.T_FBPBISPTH,
                action_flg: 'Q',
                iaryA: [iaryA]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt_a > 0) {
                    for (var i = 0; i < outObj.oaryA.length; i++){
                            SelectDom.addSelect(domObj.$route_code,outObj.oaryA[i].path_id,outObj.oaryA[i].path_id);
                    }

                }
            }
        },
        findProctByWo : function () {
            var inObj,
                outObj,
                iary,
                wo_id = domObj.$wo_id.val(),
                iary = {
                    wo_id : wo_id
                };
            inObj = {
                trx_id: VAL.T_FDPAPLYWO,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                    var mdlIdFk = outObj.oary[0].mdl_id_fk;
                    return mdlIdFk;
                }
            }
        },
        checkGrade : function (grade) {
            var inObj,
                outObj,
                iary,
                iary = {
                mdl_id : domObj.$product_code.val()
                };
            inObj = {
                trx_id: VAL.T_FBPMDLDEF,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj != null) {
                if (outObj.rtn_code == VAL.NORMAL) {
                    if (outObj.tbl_cnt > 0) {
                            if (grade != outObj.oary[0].mdl_id) {
                                showErrorDialog(oary[0].mdl_id,"此等级与产品中的不符");
                                return false;
                            }
                    }

                }
            }
        },

	};
	var btnFun = {
	    getReserList : function () {
            var inObj,
                outObj,
                iary,
                iary = {
                    rul_act_code  : $("#reservation_type").val(), //预约类型
                    mdl_id_fk	  : $("#product_code").val(),		//产品代码
                    path_id_fk		  : $("#route_code").val(),		//工艺代码
                    path_ver_fk		  : $("#route_ver").val(),		//工艺版本
                    ope_id_fk			  : $("#ope_id").val(),			//制程代码
                    deft_code		  	  : $("#bad_id").val(),			//不良
                    wo_id_fk			  : $("#wo_id").val(),			//工单号
                    tool_id_fk              : $("#tool_id").val(),          //设备
                    prd_grade             : $("#grade").val(),            //等级
                    group_id_fk			  : $("#lot_id").val(),			//批次号
                    rsn_code		  : $("#reason_id").val(),		//原因代码
                    ope_no_fk			  : $("#ope_no").val(),			//操作代码
                }
            inObj = {
                trx_id: VAL.T_FSPFUTACTION,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                    setGridInfo(outObj.oary, domObj.grids.$prdInfoGrd);
                }else {
                    setGridInfo("", domObj.grids.$prdInfoGrd);
                    showErrorDialog(null,"没有数据");
                }
            }
        },
        register : function () {
            var inObj,
                outObj,
                iary,
                act_pos_flg,
                slot_arr = [];
                if($('#inlineRadio1').is(':checked')) {
                    act_pos_flg = '0'
                } else {
                    act_pos_flg = '1';
                }
                $(":checkbox").each(function(){
                    if(this.checked == true){
                        slot_arr.push($(this).val());
                    }
                });
                iary = {
                    rul_act_code  : $("#reservation_type").val(), //预约类型
                    mdl_id_fk	  : $("#product_code").val(),		//产品代码
                    path_id_fk		  : $("#route_code").val(),		//工艺代码
                    path_ver_fk		  : $("#route_ver").val(),		//工艺版本
                    ope_id_fk			  : $("#ope_id").val(),			//制程代码
                    deft_code		  	  : $("#bad_id").val(),			//不良
                    wo_id_fk			  : $("#wo_id").val(),			//工单号
                    tool_id_fk              : $("#tool_id").val(),          //设备
                    prd_grade             : $("#grade").val(),            //等级
                    group_id_fk			  : $("#lot_id").val(),			//批次号
                    rsn_code		  : $("#reason_id").val(),		//原因代码
                    ope_no_fk			  : $("#ope_no").val(),			//操作代码
                    prd_seq_id          : $("#prd_seq_id").val(),
                    ope_note            : $("#ope_note").val(),
                    act_pos_flg     :  act_pos_flg,
                    res_mdl_id_fk   :  domObj.$res_product_code.val(),
                    res_wo_id_fk    :  domObj.$reserWorkId.val(),
                    res_path_id_fk  :  domObj.$res_path_id.val(),
                    res_path_ver_fk :  domObj.$res_path_ver.val(),
                    res_ope_no_fk   :  domObj.$res_ope_no.val(),
                    res_ope_id_fk   :  domObj.$res_ope_id.val(),
                    res_tool_id_fk  :  domObj.$res_tool.val(),
                    slot_arr : slot_arr,
                    evt_usr : VAL.EVT_USER
                };
            inObj = {
                trx_id: VAL.T_FSPFUTACTION,
                action_flg: 'A',
                iary: [iary],

            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("新增成功");
            }
        },
        active : function () {
            var rowId = domObj.grids.$prdInfoGrd
                .jqGrid("getGridParam", "selrow");
            var rowData = domObj.grids.$prdInfoGrd
                .jqGrid("getRowData", rowId);
            if(!rowData.hasOwnProperty("rul_seq_id")){
                showErrorDialog("", "请选择一条原因代码");
                return false;
            }
            var rul_seq_id = rowData.rul_seq_id;
            var inObj,
                outObj,
                iary,
                iary = {
                    rul_seq_id : rul_seq_id
                },
            inObj = {
                trx_id: VAL.T_FSPFUTACTION,
                action_flg: 'Y',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog(outObj.rtn_mesg);
            }
        },
        deleteInfo : function () {
	        var ids = [];
            var selectedIds =  domObj.grids.$prdInfoGrd.jqGrid("getGridParam", "selarrrow");
            // var rowId = domObj.grids.$prdInfoGrd
            //     .jqGrid("getGridParam", "selrow");
            for (var i = 0;i<selectedIds.length;i++){
                 var rowId = selectedIds[i];
                 var rowData = domObj.grids.$prdInfoGrd
                    .jqGrid("getRowData", rowId);
                 ids.push(rowData.rul_seq_id);
            }
            if(selectedIds.length < 1){
                showErrorDialog("", "请选择一条原因代码");
                return false;
            }
            var rul_seq_id = rowData.rul_seq_id;
            var inObj,
                outObj,
                iary,
                iary = {
                    rul_seq_ids : ids
                },
                inObj = {
                    trx_id: VAL.T_FSPFUTACTION,
                    action_flg: 'D',
                    iary: [iary]
                };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("删除成功");
            }
        },
        downSample : function () {
            if ($("#downForm").length > 0) {
                $("#downForm").remove();
            }
            var str = '<form id="downForm" action="download.do" method="post">';
            str = str + '<input type="hidden" name="fileName" id= "fileName" />';
            str = str + "</form>";

            $(str).appendTo("body");
            $("#fileName").val("2800.xls");
            $("#downForm").submit();
        },
        importId : function () {
            $("#uploadModel").modal("show");

        },


    };
	var iniContorlData = function(){
		toolFun.initReser();
		toolFun.initReason();
        toolFun.initProgram();
		//产品类型改变
		domObj.$reservation_type.change(function () {

            toolFun.clearFun(domObj.$reason_id,domObj.$res_path_id,domObj.$res_path_ver,domObj.$res_ope_no,domObj.$res_ope_id);
			toolFun.initReason();
			toolFun.initReserTyp();
            toolFun.initProgram();
            if (domObj.$product_code.val() != "" && domObj.$reservation_type.val() == "FTAJ"){
                toolFun.initResPathId();
            }
            if (domObj.$reservation_type.val() == "FTRW") {
                toolFun.checkPathId(null,"REWK");
            }
            if (domObj.$reservation_type.val() == "FTSM") {
                toolFun.checkPathId(null,"SAMP");
            }

        });
		//产品改变
		domObj.$product_code.change(function () {
            toolFun.clearFun(domObj.$route_code,domObj.$route_ver,domObj.$ope_no,domObj.$ope_id,domObj.$res_path_id,domObj.$res_product_code);
            if (domObj.$product_code.val() != ""){
                toolFun.showRoute_code();
            } else {
                toolFun.checkPathIdNoRes(null,"MAN");
                toolFun.checkPathIdNoRes(null,"RWK");
            }

            toolFun.initReserByCon();
            if (domObj.$product_code.val() != "" && domObj.$reservation_type.val() == "FTAJ"){
                toolFun.initResPathId();
            }
            // if (domObj.$reservation_type.val() == "FTRW") {
            //     toolFun.checkPathId()
            // }
        });
		//工艺路线改变
		domObj.$route_code.change(function () {
            toolFun.clearFun(domObj.$route_ver,domObj.$ope_no,domObj.$ope_id,domObj.$tool_id);
            toolFun.showRoute_ver();
            toolFun.initOpe_no();
            toolFun.initOpe_id();
            if (domObj.$ope_id.val() != null) {
                toolFun.initTool();
            }

        });
        //工艺版本改变
        domObj.$route_ver.change(function () {
            toolFun.clearFun(domObj.$ope_no,domObj.$ope_id,domObj.$tool_id);
            toolFun.initOpe_no();
            toolFun.initOpe_id();
            if (domObj.$ope_id.val() != null) {
                toolFun.initTool();
            }
        });
        domObj.$ope_id.change(function () {
            toolFun.clearFun(domObj.$tool_id);
            if (domObj.$ope_id.val() != null) {
                toolFun.initTool();
            }
        });
        //操作代码改变
		domObj.$ope_no.change(function () {
            toolFun.clearFun(domObj.$ope_id);
			toolFun.initOpe_id();

        });
		//制程改变
        domObj.$ope_id.change(function () {
            toolFun.clearFun(domObj.$tool_id);
            toolFun.initOpe_id();
            if (domObj.$ope_id.val() != null) {
                toolFun.initTool();
            }
        })
		//不良输入
		domObj.$bad_id.blur(function () {
			if ($.trim(domObj.$bad_id.val()) != ""){
                toolFun.checkBad();
			}
        });
        //工单号
        domObj.$wo_id.blur(function () {
            if ($.trim(domObj.$wo_id.val()) != ""){
                domObj.$res_product_code.empty();
                SelectDom.addSelect(domObj.$res_product_code,"","");
                toolFun.checkWoId();
            }
            toolFun.initReserByCon();
        });
        //产品唯一码
		domObj.$prd_seq_id.blur(function () {
			if ($.trim(domObj.$prd_seq_id.val()) != ""){
                toolFun.checkPrdId();
			}
        });
		//预约工单号
		domObj.$reserWorkId.blur(function () {
		    if (domObj.$product_code.val() != "" && domObj.$reserWorkId.val() != ""){
                toolFun.checkResWoId();
            } else {
                return false;
                showErrorDialog("", "请输入产品代码");
            }

        });
		//预约工艺路线改变
		domObj.$res_path_id.change(function () {
            toolFun.clearFunNoSpace(domObj.$res_path_ver,domObj.$res_ope_no,domObj.$res_ope_id);
            toolFun.initResPathVer();
            toolFun.initResOpeNo();
            toolFun.initResOpe_id();
        });
        //预约工艺版本改变
        domObj.$res_path_ver.change(function () {
            toolFun.clearFunNoSpace(domObj.$res_ope_no,domObj.$res_ope_id);
            toolFun.initResOpeNo();
            toolFun.initResOpe_id();
        });
        //预约操作diamante改变
        domObj.$res_ope_no.change(function () {
            toolFun.clearFunNoSpace(domObj.$res_ope_id);
            toolFun.initResOpe_id();
        });
        //预约制程改变
        domObj.$res_ope_id.change(function () {
            if (domObj.$res_ope_id.val() != null) {
                toolFun.initResTool();
            }
        });




    };
	var iniButtonAction = function() {
	    //获取预约列表
	    domObj.buttons.$reser_list.click(function () {
            setGridInfo(null, domObj.grids.$prdInfoGrd);
            btnFun.getReserList();
        });
        //登记
        domObj.buttons.$login_btn.click(function () {
            if(domObj.$grade.val() != ""){
                toolFun.checkGrade(domObj.$grade.val());
                return false;
            }
            if (domObj.$prd_seq_id.val() != ""){
                btnFun.register();
                btnFun.getReserList();
            } else {
                showSuccessDialog("请输入产品唯一码");
                return false;
            }
        });
        //激活
        domObj.buttons.$active_btn.click(function () {
            btnFun.active();
            btnFun.getReserList();
        });
        //删除
        domObj.buttons.$remove_btn.click(function () {
            btnFun.deleteInfo();
            btnFun.getReserList();
        });
        //导入模版下载
        $("#down").click(function(){
           btnFun.downSample();
        });
        //导入
        $("#import").click(function () {
            btnFun.importId();
        });

       $("#uploadModel").on("click","#uploadBtn",function(){
           $("#uploadForm").submit();

        })


	};

	var initFun = function fs() {
		initPrdInfoGrd();
		iniContorlData();
		iniButtonAction();

	}();


    //表格自适应
    function resizeFnc(){
        var offsetBottom, divWidth;
        divWidth = $("#prdInfoGrdDiv").width();
        offsetBottom =  $(window).height() - $("#prdInfoGrdDiv").offset().top-20;
        $("#prdInfoGrdDiv").height(offsetBottom * 0.95);
        $("#prdInfoGrd").setGridWidth(divWidth * 0.99);
        $("#prdInfoGrd").setGridHeight(offsetBottom * 0.95 - 81);
    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });















		});